package com.myz.rabbitmq.controller;

import com.myz.rabbitmq.config.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

// 开始发消息 测试确认
@Slf4j
@RestController
@RequestMapping("/confirm")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    // 发消息
    @GetMapping("/sendMessage/{message}")
    public void sendMessage(@PathVariable String message){
        // 发送一个正确的消息
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString().replaceAll("-",""));
        log.info("发送消息为:{}",message);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE,ConfirmConfig.CONFIRM_ROUTINGKEY,message,correlationData);
        // 发送一个错误的消息
        CorrelationData correlationData2 = new CorrelationData(UUID.randomUUID().toString().replaceAll("-",""));
        log.info("发送消息为:{}",message+"2");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE,ConfirmConfig.CONFIRM_ROUTINGKEY+"2",message+"2",correlationData2);
    }
}
